import {login, changePwd} from "libs/api/user"
import {setCookie} from "libs/cookie"

export const USER_INFO = "USER_INFO"
export const LOGIN_OUT = "LOGIN_OUT"
export const CHANGE_PWD = "CHANGE_PWD"

export function signin(info) {
  return async (dispatch) => {
    let {data, eCode} = await login(info)
    if (data) {
      setCookie("token", data.token)
      dispatch({
        type: USER_INFO,
        data
      })
    } else {
      return eCode
    }
  }
}
export function userLogOut() {
  return (dispatch) => {
    setCookie("token", "", 0)
    dispatch({
      type: LOGIN_OUT
    })
  }
}
export function updatePassword(info) {
  return async (dispatch) => {
    let {data, message} = await changePwd(info)
    if (data) {
      setCookie("token", "", 0)
      dispatch({
        type: CHANGE_PWD,
        data
      })
    } else {
      return message
    }
  }
}